//wifi
#ifdef ARDUINO_ARCH_ESP32
  #include <WiFi.h>
#elif defined(ARDUINO_ARCH_ESP8266)
  #include <ESP8266WiFi.h>
#endif

//打印日志
void logWifi(){
  // WiFi连接成功后将通过串口监视器输出连接成功信息
  // WiFi名称
  Serial.print("[MyWifi] Connected to ");
  Serial.println(WiFi.SSID());
  // IP
  Serial.print("[MyWifi] IP address: ");
  Serial.println(WiFi.localIP());
}

//初始化连接wifi
void initWifi(char* ssid, char* password){
  //连接网络
  WiFi.begin(ssid, password);
  //打印日志
  Serial.print("[MyWifi] connecting WiFi...");
  //等待网络连接成功
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  //打印日志
  Serial.println("");
  Serial.print("[MyWifi] ESP Connected to ");
  Serial.println(WiFi.SSID());              // WiFi名称
  //打印地址
  Serial.print("[MyWifi] IP address: ");
  Serial.println(WiFi.localIP()); //打印模块IP
}

//初始化连接wifi-可以设置hostname
void initWifi(char* ssid, char* password, char* hostname){
  initWifi(ssid, password);
  //设置主机名
  WiFi.setHostname(hostname);
  //打印主机名
  Serial.print("[MyWifi] HostName: ");
  Serial.println(WiFi.getHostname());
}

//获取wifi状态
bool wifiState(){
  return WiFi.isConnected();
}